package handlers

import (
  "encoding/json"
  "github.com/cloudwego/hertz/pkg/common/hlog"
  "github.com/hertz-contrib/sse"
  "strings"
)

func printResponseContent(msg *sse.Event, completeContent *strings.Builder) {
  var responseData struct {
    Choices []struct {
      Delta struct {
        Content string `json:"content"`
      } `json:"delta"`
    } `json:"choices"`
  }
  if len(msg.Data) > 6 {
    if err := json.Unmarshal(msg.Data, &responseData); err == nil {
      for _, choice := range responseData.Choices {
        //hlog.Info("content:", choice.Delta.Content)
        completeContent.WriteString(choice.Delta.Content)
      }
    } else {
      hlog.Error("json parsing error: %s", err)
    }
  }
  // Check if it's the end of the data
  if string(msg.Data) == "[DONE]" {
    hlog.Info("Complete content:", completeContent.String())
    completeContent.Reset() // Clear the builder for the next message
  }
}
